/* Copyright (c) 2009 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.haojii.notifier.easytv;

import java.util.List;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.persistence.EntityManager;

import com.haojii.notifier.helper.EMF;
import com.haojii.notifier.helper.PMF;
import com.haojii.notifier.user.User;

public class EasytvEntityUtils {

	private static final Logger log = Logger.getLogger(EasytvEntityUtils.class.getName());
	
	public static List<EasytvEntity> list()
	{
		PersistenceManager pm = PMF.get().getPersistenceManager();
	
		Query query = pm.newQuery(EasytvEntity.class);
		List<EasytvEntity> es = (List<EasytvEntity>) query.execute();
		List<EasytvEntity> result = (List<EasytvEntity>)pm.detachCopyAll(es);
		pm.close();
		return result;
	}
	
	public static EasytvEntity get(String url)
	{
		EasytvEntity e=null,detached=null;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try{
			e = pm.getObjectById(EasytvEntity.class, url);
			detached = pm.detachCopy(e); 
		}
		catch(Exception ex)
		{
			
		}
		finally{
			pm.close();
		}
		return detached;
	}
	
	public static void save(EasytvEntity e)
	{
		log.info("save:"+e.getTitle()+" "+e.getUrl()+" itemsize:"+e.getItems().size());
		PersistenceManager pm = PMF.get().getPersistenceManager();
		pm.makePersistent(e);
		pm.close();
	}
	
	public static void update(EasytvEntity e)
	{
		log.info("update:"+e.getTitle()+" "+e.getUrl()+" itemsize:"+e.getItems().size());
		PersistenceManager pm = PMF.get().getPersistenceManager();
		EasytvEntity dest = pm.getObjectById(EasytvEntity.class, e.url);
		if(e.getItems().size()>dest.getItems().size())
		{
			dest.getItems().clear();
			dest.getItems().addAll(e.getItems());
		}
		pm.close();
	}
	
	public static void delete(String urlKey)
	{
		log.info("delete:"+urlKey);
		PersistenceManager pm = PMF.get().getPersistenceManager();
		EasytvEntity dest = pm.getObjectById(EasytvEntity.class, urlKey);
		pm.deletePersistent(dest);
		pm.close();
	}
	
	private void doPostJDO(long key, String orig, String dest) {
	    PersistenceManager pm = PMF.get().getPersistenceManager();
	    try {
	      EasytvEntity f = pm.getObjectById(EasytvEntity.class, key);
	      
	    } finally {
	      pm.close();
	    }
	  }

	  private void doPostJPA(long key, String orig, String dest) {
	    EntityManager em = EMF.get().createEntityManager();
	    try {
	      EasytvEntity f = em.find(EasytvEntity.class, key);
	      
	    } finally {
	      em.close();
	    }
	  }
}
